#include "../config/tinyxml2/tinyxml2.h"

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <cstdint>
#include <fstream>

#include "ptp.h"

using namespace tinyxml2;

// 从XML文件读取TSN配置
bool readTSNConfig( std::string &filename, TSNConfig &config)
{
    XMLDocument doc;

    // 加载XML文件
    if (doc.LoadFile(filename.c_str()) != XML_SUCCESS)
    {
        std::cerr << "加载文件失败: " << filename << std::endl;
        return false;
    }

    // 获取根元素
    XMLElement *root = doc.RootElement();
    if (root == nullptr)
    {
        std::cerr << "没有根元素" << std::endl;
        return false;
    }

    // 获取"TSN_cfg"元素
    XMLElement *tsn_cfg = root->FirstChildElement("TSN_cfg");
    if (tsn_cfg == nullptr)
    {
        std::cerr << "没有'TSN_cfg'元素" << std::endl;
        return false;
    }

    // 读取并存储配置信息
    if (tsn_cfg->FirstChildElement("device_id") == nullptr ||
        tsn_cfg->FirstChildElement("gm_flag") == nullptr ||
        tsn_cfg->FirstChildElement("port_num") == nullptr ||
        tsn_cfg->FirstChildElement("master_port") == nullptr ||
        tsn_cfg->FirstChildElement("slave_port") == nullptr ||
        tsn_cfg->FirstChildElement("link_delay_measure_period") == nullptr ||
        tsn_cfg->FirstChildElement("sync_period") == nullptr ||
        tsn_cfg->FirstChildElement("domain_number") == nullptr ||
        tsn_cfg->FirstChildElement("mean_link_delay_thrld") == nullptr)
    {
        std::cerr << "缺少某些必要的配置元素" << std::endl;
        return false;
    }

    tsn_cfg->FirstChildElement("device_id")->QueryIntText(&config.device_id);
    tsn_cfg->FirstChildElement("gm_flag")->QueryIntText(&config.gm_flag);
    tsn_cfg->FirstChildElement("port_num")->QueryIntText(&config.port_num);
    tsn_cfg->FirstChildElement("master_port")->QueryIntText(&config.master_port);
    tsn_cfg->FirstChildElement("slave_port")->QueryIntText(&config.slave_port);
    tsn_cfg->FirstChildElement("link_delay_measure_period")->QueryIntText(&config.link_delay_measure_period);
    tsn_cfg->FirstChildElement("sync_period")->QueryIntText(&config.sync_period);
    tsn_cfg->FirstChildElement("domain_number")->QueryIntText(&config.domain_number);

    XMLElement *port_identity = tsn_cfg->FirstChildElement("port_identity");
    if (port_identity == nullptr)
    {
        std::cerr << "缺少'port_identity'元素" << std::endl;
        return false;
    }

    XMLElement *clockElem = port_identity->FirstChildElement("clockIdentity");
    if (clockElem)
    {
        const char *text = clockElem->GetText();
        if (text)
        {
            std::stringstream ss(text);
            std::string item;
            int index = 0;

            while (getline(ss, item, ',') && index < 8)
            {
                size_t pos;
                unsigned long value = stoul(item, &pos, 10);
                if (pos == item.size() && value <= UINT8_MAX)
                {
                    config.port_identity.clockIdentity[index++] = static_cast<uint8_t>(value);
                }
                else
                {
                    std::cerr << "解析clockIdentity值时出错: " << item << std::endl;
                    break;
                }
            }

            for (; index < 8; ++index)
            {
                config.port_identity.clockIdentity[index] = 0;
            }
        }
    }
    else
    {
        std::cerr << "缺少'clockIdentity'元素" << std::endl;
        return false;
    }

    port_identity->FirstChildElement("portNumber")->QueryIntText(&config.port_identity.portNumber);
    tsn_cfg->FirstChildElement("mean_link_delay_thrld")->QueryIntText(&config.mean_link_delay_thrld);

    return true;
}